﻿
function MainFrame() {
    return $(window.parent.document).contents().find("#frmPage")[0].contentWindow;
}
function DialogFrame(name) {
    return $(window.parent.document).contents().find("#" + name + "_content")[0].contentWindow;
}
function showloading() {
    $("#loading").show();
}
function hideloading() {
    $("#loading").hide();
}
function showparentloading() {
    parent.$("#loading").show();
}
function hideparentloading() {
    parent.$("#loading").hide();
}
function showtip(option, strhtml) {
    var w = option.width == undefined ? 200 : option.width;
    var h = option.height == undefined ? 120 : option.height;
    $("#qq_content").css("width", w + "px").css("height", h + "px");
    $("#qq_tip").css("width", w + 14 + "px").css("height", h + 33 + "px").css("bottom", -(h + 33) + "px");
    $("#qq_content").html(strhtml);

    $("#qq_tip").animate({ bottom: "5px" }, 1000, function () { });
}
function hidetip() {
    $("#qq_tip").animate({ bottom: -$(this).height() }, 1000, function () { });
}

function GetCycleName(cycle) {
    switch (cycle) {
        case "01":
            return "月报"
            break;
        case "02":
            return "季报";
            break;
        case "03":
            return "年报";
            break;
    }
}

function DateTimeStr(cycle, str_year, str_month, str_day) {
    switch (cycle) {
        case "01":
            return str_year + "年" + str_month + "月";
            break;
        case "02":
            return str_year + "年第" + str_month.substr(1, 1) + "季度";
            break;
        case "03":
            return str_year + "年";
            break;
    }

}

jQuery.extend({
    funDetailSave: function (formId, editUrl, callBack) {
        //验证表单
        if (typeof ($("#" + formId).valid) == "function" && !$("#" + formId).valid()) {
            return;
        }


        var postData = {
            detail: $("#" + formId).serialize()
        };
        editUrl = editUrl + "?rid=" + Math.random() * 100000000;
        $.post(editUrl, postData, function (d) {
            if (typeof (callBack) == 'function') {
                callBack(d);
            }

        });

    },
    funDelete: function (editUrl, postData, callBack) {

        editUrl = editUrl + "?rid=" + Math.random() * 100000000;
        $.post(editUrl, postData, function (d) {
            if (typeof (callBack) == 'function') {
                callBack(d);
            }

        });

    }
});
/********************************/


var color = ["", "FDC12E", "56B9F9", "C9198D", "AFD8F8", "F6BD0F", "008E8E", "9D080D", "8BBA00", "D64646", "8E468E", "588526", "B3AA00", "008ED6", "FF8E46", "A186BE"];
var singleName = ["2D柱状图", "3D柱状图", "2D饼状图", "3D饼状图", "折线图", "条形图", "面积图", "2D圆环图", "3D圆环图"];
var singleUrl = ["Column2D.swf", "Column3D.swf", "Pie2D.swf", "Pie3D.swf", "Line.swf", "Bar2D.swf", "Area2D.swf", "Doughnut2D.swf", "Doughnut3D.swf"];
var multiName = ["2D柱状图", "3D柱状图", "2D柱状堆积图", "3D柱状堆积图", "折线图", "条形图", "条形堆积图", "面积图", "面积堆积图", "两轴线柱图"];
var multiUrl = ["MSColumn2D.swf", "MSColumn3D.swf", "StackedColumn2D.swf", "StackedColumn3D.swf", "MSLine.swf", "MSBar2D.swf", "StackedBar2D.swf", "MSArea2D.swf", "StackedArea2D.swf", "MSCombiDY2D.swf"];
var reValue = "1|1|2|2|2|2|x|0|1";
var maxvalue = -999999999;
var targetmaxvalue = -999999999;
var oldobj = null;
var strXML = "";
var isshowchart = false;
var _ChartType = "";
var rowhead = "", colhead = "", rowheadstart = "", rowheadend = "", colheadstart = "", colheadend = "", showhead = "x", showdata = "0";
function showchart() {
    //根据表格分析开始、结束列。
    rowhead =1;
    colhead = $("#TableMain tr:eq(0) td[sx='wd_title']").eq(0).index() + 1;
    rowheadstart = $("#TableMain tr:eq(0) td[sx='zb']").eq(0).index() + 1;
    rowheadend = $("#TableMain tr:eq(0) td[sx='zb']").last().index() + 1;
    colheadstart = $("#TableMain td[sx='wd']").first().parent().index() + 1;
    colheadend = $("#TableMain td[sx='wd']").last().parent().index() + 1;

    parent.asyncbox.open({
        url: "/Scripts/ChartSet.htm?id=" + Math.random(),
        width:300,
        height: 150,
        scroll: 'no',
        title: "图表设置",
        modal: true,
        btnsbar: parent.$.btn.OKCANCEL, //按钮栏配置请参考 “辅助函数” 中的 $.btn。
        callback: function (action, opener) {
            //判断 action 值。
            if (action == 'ok') {
//                rowhead = opener.document.getElementById("rowheadnum").value;

//                colhead = opener.document.getElementById("columnheadnum").value;
//                rowheadstart = opener.document.getElementById("startcolumn").value == "" ? 2 : opener.document.getElementById("startcolumn").value;
//                 rowheadend = opener.document.getElementById("endcolumn").value == "" ? $("#TableMain tr:eq(1) td").length : opener.document.getElementById("endcolumn").value;

//                 colheadstart = opener.document.getElementById("startrow").value == "" ? 2 : opener.document.getElementById("startrow").value;
//                 colheadend = opener.document.getElementById("endrow").value == "" ? $("#TableMain tr").length : opener.document.getElementById("endrow").value;

                
                if (opener.document.getElementById("ht").checked) {
                    showhead = "x";
                }
                else {
                    showhead = "y";
                }
                showdata = opener.document.getElementById("showdata").checked == true ? "1" : "0";

                var returnValue = rowhead + "|" + colhead + "|" + rowheadstart + "|" + rowheadend + "|" + colheadstart + "|" + colheadend + "|" + showhead + "|" + showdata + "|1";
//                isshowchart = true;
                chart(returnValue);

            }
            if (action == 'cancel') {

            }
            if (action == 'close') {

            }
        }
    });

}

function chart(returnValue) {

    var arr = returnValue.split("|");
    var rowhead = parseInt(arr[0]) - 1;
    var colhead = parseInt(arr[1]) - 1;
    var rowheadstart = parseInt(arr[2]);
    var rowheadend = parseInt(arr[3]);

    var colheadstart = parseInt(arr[4]);
    var colheadend = parseInt(arr[5]);
    var showhead = arr[6];
    var showdata = arr[7];
    var isdialog = arr[8];
    var targetValue = arr[9];
    var strXml = "";
    var swfType = "";
    var isshowchart = 0;
    var colorIndex = 0;
   
    //$("#TableMain tr:eq(" + rowhead + ") td:eq("+i+")")
    strXml += "<graph  imageSave='1' exportHandler='ImageSave'     exportDialogMessage='正在生成图表,请稍后...'  caption='' yAxisName='金额'    yAxisName='测试' decimalPrecision='2' formatNumberScale='0' baseFontSize='12' showNames='1' showValues ='" + showdata + "' exportDialogColor='e1f5ff' exportDialogBorderColor='0372ab' exportDialogFontColor='0372ab' exportDialogPBColor='0372ab'  tttt>";
    if (showhead == "x") {
        if (colheadend - colheadstart >= 0) {
            swfType = "MS";
            strXml += " <categories>";

            var tempcount=0
            for (var i = rowheadstart - 1; i < rowheadend; i++) {
                strXml += '<category name="' + $("#TableMain tr:eq(" + rowhead + ") td[sx='zb']:eq(" + tempcount + ")").attr("zbname") + '"/>\r\n';
                tempcount = parseInt(tempcount) + 1;
            }
            strXml += "</categories>";

            for (var i = colheadstart - 1; i < colheadend; i++) {
                if (i == colheadstart - 1) {
                    colorIndex = i;
                }

                if (_ChartType == "MSCombiDY2D" && i > 2) {

                    strXml += "<dataset seriesname='" + $("#TableMain tr:eq(" + i + ") td[sx='wd']:eq(0)").attr("wdname") + "' color='" + color[i] + "' parentYAxis='S'>";
                }
                else {
                    strXml += "<dataset seriesname='" + $("#TableMain tr:eq(" + i + ") td[sx='wd']:eq(0)").attr("wdname") + "' color='" + color[i] + "'>";
                }
               var tempcount2=0;
                for (var j = rowheadstart - 1; j < rowheadend; j++) {

                    var aa = $("#TableMain tr:eq(" + i + ") td[sx='data']:eq(" + tempcount2 + ")").text().replace(/(^\s*)|(\s*$)/g, "").replace(/(^\s*)/g, "").replace(/(\s*$)/g, "") == "" ? "" : $("#TableMain tr:eq(" + i + ") td[sx='data']:eq(" + tempcount2 + ")").text().replace(/\,/g, "");
                    tempcount2 = parseInt(tempcount2) + 1; 
                    if (aa != "")
                        isshowchart =true;
                    strXml += '<set  value="' + aa + '"/>\r\n';

                    maxvalue = maxvalue > parseFloat(aa == "" ? "-99999999" : aa) ? maxvalue : parseFloat(aa);
                }
                strXml += "</dataset>";
            }
        }
//        else {//对应这个 if (colheadend - colheadstart > 0)  if语句

//            for (var i = colheadstart - 1; i < colheadend; i++) {
//                var tempcount3 = 0;
//                for (var j = rowheadstart - 1; j < rowheadend; j++) {
//                    var aa = ($("#TableMain tr:eq(" + i + ") td[sx='data']:eq(" + tempcount3 + ")").text().replace(/(^\s*)|(\s*$)/g, "").replace(/(^\s*)/g, "").replace(/(\s*$)/g, "") == "" ? "" : $("#TableMain tr:eq(" + i + ") td[sx='data']:eq(" + tempcount3 + ")").text()).replace(/\,/g, "");
//                    if (aa != "")
//                        isshowchart = 1;
//                    if (j == rowheadstart - 1) {
//                        colorIndex = j;
//                    }
//                    strXml += '<set name="' + $("#TableMain tr:eq(" + rowhead + ") td:eq(" + j + ")").text() + '" value="' + aa + '"  fontSize="12" color="' + color[j] + '"/>\r\n';
//                    maxvalue = maxvalue > parseFloat(aa == "" ? "-99999999" : aa) ? maxvalue : parseFloat(aa);
//                }
//            }
//        }
    }
    else {//对应 if (showhead == "x")  这个if语句

        if (rowheadend - rowheadstart >= 0) {
            swfType = "MS";
            strXml += " <categories>";
            for (var i = colheadstart - 1; i < colheadend; i++) {
                strXml += '<category name="' + $("#TableMain tr:eq(" + i + ") td[sx='wd']:eq(0)").attr("wdname") + '"/>\r\n';
            }
            strXml += "</categories>";
            var tempcount = 0;
            for (var i = rowheadstart - 1; i < rowheadend; i++) {
                if (i == rowheadstart - 1) {
                    colorIndex = i;
                }

                strXml += "<dataset seriesname='" + $("#TableMain tr:eq(" + rowhead + ") td[sx='zb']:eq(" + tempcount + ")").attr("zbname") + "' color='" + color[i] + "'>";
                
               
                for (var j = colheadstart - 1; j < colheadend; j++) {

                    var aa = $("#TableMain tr:eq(" + j + ") td[sx='data']:eq(" + tempcount + ")").text().replace(/(^\s*)|(\s*$)/g, "").replace(/(^\s*)/g, "").replace(/(\s*$)/g, "") == "" ? "" : $("#TableMain tr:eq(" + j + ") td[sx='data']:eq(" + tempcount + ")").text().replace(/\,/g, "");
                    if (aa != "")
                        isshowchart = 1;
                    strXml += '<set  value="' + aa + '"/>\r\n';

                    maxvalue = maxvalue > parseFloat(aa == "" ? "-99999999" : aa) ? maxvalue : parseFloat(aa);
                }
                strXml += "</dataset>";
                tempcount = parseInt(tempcount) + 1;
            }
        }
//        else {
//            for (var i = rowheadstart - 1; i < rowheadend; i++) {
//                for (var j = colheadstart - 1; j < colheadend; j++) {
//                    var aa = ($("#TableMain tr:eq(" + j + ") td:eq(" + i + ")").text().replace(/(^\s*)|(\s*$)/g, "").replace(/(^\s*)/g, "").replace(/(\s*$)/g, "") == "" ? "" : $("#TableMain tr:eq(" + j + ") td:eq(" + i + ")").text()).replace(/\,/g, "");
//                    if (aa != "")
//                        isshowchart = 1;

//                    if (j == colheadstart - 1) {
//                        colorIndex = j;
//                    }
//                    strXml += '<set name="' + $("#TableMain tr:eq(" + j + ") td:eq(" + colhead + ")").text() + '" value="' + aa + '"  fontSize="12" color="' + color[j] + '"/>\r\n';
//                    maxvalue = maxvalue > parseFloat(aa == "" ? "-99999999" : aa) ? maxvalue : parseFloat(aa);
//                }
//            }
//        }
    }
    //    if (gstrShowCaution == "1") {
    //        // strXml += "<trendlines> <line startValue='" + gdblCautionValue + "'   color='ff0000' displayValue='目标值:" + gdblCautionValue + "' dashed='0' thickness='2' dashGap='2' alpha='100' showOnTop='' /> </trendlines>";

    //    }

    if (targetValue == "" || targetValue == undefined) {
    }
    else {
        var arr = targetValue.split("~");
        strXml += "<trendlines> ";
        for (var i = 0; i < arr.length - 1; i++) {

            var mbarr = arr[i].split(",");
            if (mbarr[3].replace(/(^\s*)|(\s*$)/g, "").replace(/(^\s*)/g, "").replace(/(\s*$)/g, "") == "" || mbarr[3].replace(/(^\s*)|(\s*$)/g, "").replace(/(^\s*)/g, "").replace(/(\s*$)/g, "") == 0) {
            }
            else {
                var startvalue = mbarr[3].replace(/(^\s*)|(\s*$)/g, "").replace(/(^\s*)/g, "").replace(/(\s*$)/g, "");
                strXml += "<line startValue='" + startvalue + "'   color='" + color[colorIndex] + "' displayValue='" + mbarr[2] + ":" + mbarr[3].replace(/(^\s*)|(\s*$)/g, "").replace(/(^\s*)/g, "").replace(/(\s*$)/g, "") + "' dashed='0' thickness='2' dashGap='2' alpha='100' showOnTop='' /> "
                targetmaxvalue = targetmaxvalue > startvalue ? targetmaxvalue : startvalue;

            }
            colorIndex++;
        }
        strXml += "</trendlines>"

    }
    strXml += "</graph>";

    if (targetmaxvalue > maxvalue) {
        targetmaxvalue = parseFloat(targetmaxvalue);
        if (targetmaxvalue > 10000) {
            targetmaxvalue = targetmaxvalue + 10000;
        }
        else if (targetmaxvalue > 1000) {
            targetmaxvalue = targetmaxvalue + 1000;
        }
        else if (targetmaxvalue > 100) {
            targetmaxvalue = targetmaxvalue + 100;
        }
        else if (targetmaxvalue > 10) {
            targetmaxvalue = targetmaxvalue + 10;
        }


        strXml = strXml.replace("tttt", "yAxisMaxValue='" + targetmaxvalue + "'");
    }
    else {
        strXml = strXml.replace("tttt", "");
    }

    var ismultity = false;
    if (swfType == "MS")
        ismultity = true

    if (isdialog == "1") {
        swfType = swfType + "Column3D";
    }
    else {
        swfType = _ChartType;
    }
    reloadData(strXml, swfType, ismultity);

}


function updateChart(chartSWF, obj) {
    if (oldobj != null)
        oldobj.className = "btndiv";
    obj.className = "btnselect";
    oldobj = obj;
    var chart1 = new FusionCharts("/Content/swf/" + chartSWF, "chart1Id", "700", "350");

    chart1.setDataXML(strXML);
    chart1.render("chart_container");
}
function reloadData(strxml, type, ismultity) {

    strXML = strxml;
    var swfType = type;
    var strhtml = "";


    if (ismultity == false) {
        for (var i = 0; i < singleName.length; i++) {
            strhtml += "<span class='btndiv'   onclick=\"updateChart('" + singleUrl[i] + "',this)\">" + singleName[i] + "</span>";
        }
    }
    else {
        for (var i = 0; i < multiName.length; i++) {
            strhtml += "<span  class='btndiv'    onclick=\"updateChart('" + multiUrl[i] + "',this)\">" + multiName[i] + "</span>";
        }
    }


    document.getElementById("chart_btndiv").innerHTML = strhtml;

    var chart1 = new FusionCharts("/Content/swf/" + swfType + ".swf", "chart1Id", $("#TableMain").width() > 800 ? $("#TableMain").width() : "800", $("#TableMain").height() > 480 ? $("#TableMain").height() : "480", "0", "0");
    chart1.setDataXML(strxml);
    chart1.render("chart_container");
}